home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / QuickTime / MCPlayMovie / MCPlayMovie.c next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  11.2 KB  |  469 lines  |  [TEXT/KAHL]

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                            */
  3. /*        MCPlayMovie.                                                        */
  4. /*            by John Wang                                                    */
  5. /*                                                                            */
  6. /*        Description:    This program demonstrates playing movies with        */
  7. /*            QuickTime movie controllers.  It also allows the user to save    */
  8. /*            the movies in a flattened format using FlattenMovie().            */
  9. /*                                                                            */
  10. /*        Version:        1.0 Completed 11/17/91                                */
  11. /*                        1.1 Added palette support so that app restores        */
  12. /*                            color table when changed by another app.        */
  13. /*                                                                            */
  14. /*--------------------------------------------------------------------------*/
  15.  
  16. #include     <GestaltEqu.h>
  17. #include    <Palette.h>
  18. #include    "Movies.h"
  19.  
  20. #define Gestalttest        0xA1AD
  21. #define NoTrap            0xA89F
  22.  
  23. #define    appleID            128            
  24. #define    appleMenu        0
  25. #define    aboutMeCommand    1
  26.  
  27. #define    fileID            129
  28. #define openCommand        1
  29. #define    flattenCommand    2
  30. #define closeCommand    3
  31. #define    quitCommand     5
  32.  
  33. #define    aboutMeDLOG        128
  34. #define    okButton        1
  35.  
  36. #define    MAXWINDOWS        5
  37.  
  38. /*------------------------------------------------------*/
  39. /*    Global Variables.                                    */
  40. /*------------------------------------------------------*/
  41.  
  42. Boolean                DoneFlag = FALSE;
  43. MenuHandle            mymenu0, mymenu1;
  44. Boolean                playingMovie[MAXWINDOWS];
  45. Movie                myMovie[MAXWINDOWS];
  46. WindowPtr            myWindow[MAXWINDOWS];
  47. MovieController        mcPlay[MAXWINDOWS];
  48. int                    startlocation;
  49. CTabHandle            mycolors;
  50. PaletteHandle        srcPalette;
  51.  
  52. /*------------------------------------------------------*/
  53. /*    getMovieFromFile().                                    */
  54. /*------------------------------------------------------*/
  55.  
  56. Movie getMovieFromFile()
  57. {
  58.     OSErr                    err;
  59.     StandardFileReply        reply;
  60.     Point                    where = {200, 50};
  61.     SFTypeList                types;
  62.     short                    movieResRefNum;
  63.     short                    actualResId;
  64.     Movie                    theMovie;
  65.     
  66.     types[0] = 'MooV';
  67.     StandardGetFilePreview(nil, 1, types, &reply);
  68.     if (!reply.sfGood) return((Movie) 0);
  69.  
  70.     err = OpenMovieFile(&reply.sfFile, &movieResRefNum, fsRdPerm);
  71.     if (GetMoviesError()) return((Movie) 0);
  72.     if (err) return ((Movie) 0);
  73.     actualResId = DoTheRightThing;
  74.  
  75.     err = NewMovieFromFile(&theMovie, movieResRefNum, &actualResId, (char *) 0, newMovieActive, (Boolean *) 0);
  76.     if (GetMoviesError()) return((Movie) 0);
  77.     if (err) return ((Movie) 0);
  78.  
  79.     err = CloseMovieFile(movieResRefNum);
  80.     if (GetMoviesError()) return((Movie) 0);
  81.     if (err) return ((Movie) 0);
  82.  
  83.     return (theMovie);
  84. }
  85.  
  86. /*------------------------------------------------------*/
  87. /*    playMovie().                                            */
  88. /*------------------------------------------------------*/
  89.  
  90. OSErr playMovie(int    index)
  91. {
  92.     Rect            movieBounds;
  93.     
  94.     GetMovieBox(myMovie[index], &movieBounds);
  95.     OffsetRect(&movieBounds, -movieBounds.left, -movieBounds.top);
  96.     SetMovieBox(myMovie[index], &movieBounds);
  97.     OffsetRect(&movieBounds, startlocation, startlocation);
  98.  
  99.     
  100.  
  101.     myWindow[index] = NewCWindow(0L, &movieBounds, "\PMovie!", 1, 0, (WindowPtr) -1, FALSE, 0L);
  102.     SetPalette ((WindowPtr) myWindow[index], srcPalette, TRUE);
  103.  
  104.     OffsetRect(&movieBounds, -startlocation, -startlocation);
  105.     startlocation += 50;
  106.     if (startlocation > 300) startlocation = 50;
  107.     
  108.     SetMovieGWorld(myMovie[index], (CGrafPtr) myWindow[index], 0);
  109.     if (GetMoviesError()) DebugStr("\PSetMovieGWorld error.");;
  110.     mcPlay[index] = NewMovieController(myMovie[index], &movieBounds, mcTopLeftMovie);
  111.     MCGetControllerBoundsRect(mcPlay[index], &movieBounds);
  112.     SizeWindow(myWindow[index], movieBounds.right - movieBounds.left,
  113.                             movieBounds.bottom - movieBounds.top, TRUE);
  114. /*    Uncomment these lines if you want to pre load the movie into ram.
  115.     Don't forget to increase the partition size when recompiling.
  116.     
  117.     GotoBeginningOfMovie(myMovie[index]);
  118.     if (LoadMovieIntoRam(myMovie[index], GetMovieTime(myMovie[index], 0L),
  119.                                     GetMovieDuration(myMovie[index]),
  120.                                     0) != noErr)
  121.         DebugStr("\PNot enough memory to load movie into ram.");
  122. */
  123. }
  124.  
  125. /*------------------------------------------------------*/
  126. /*    flatten().                                            */
  127. /*------------------------------------------------------*/
  128.  
  129. short flatten(int index)
  130. {
  131.     StandardFileReply        reply;
  132.     OSErr       theErr      = noErr;
  133.  
  134.     StandardPutFile("\PName of flattened movie.", "\PUntitled", &reply);
  135.     if (!reply.sfGood) return;
  136.  
  137.      if (theErr = GetMoviesError()) 
  138.         DebugStr("\PCall Before FlattenMovies failed.");
  139.  
  140.     FlattenMovie(myMovie[index],
  141.                 flattenAddMovieToDataFork,
  142.                 &reply.sfFile,
  143.                 'JWJW',
  144.                 0,
  145.                 createMovieFileDeleteCurFile,
  146.                 nil,
  147.                 nil);
  148.                 
  149.     if (theErr = GetMoviesError()) 
  150.         DebugStr("\PFlattenMovies failed.");
  151.   
  152.     return(theErr);
  153. }
  154.  
  155. /*------------------------------------------------------*/
  156. /*    showAboutMeDialog()                                    */
  157. /*------------------------------------------------------*/
  158.  
  159. void showAboutMeDialog()
  160. {
  161.     GrafPtr     savePort;
  162.     DialogPtr    theDialog;
  163.     short        itemHit;
  164.  
  165.     GetPort(&savePort);
  166.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  167.     SetPort(theDialog);
  168.  
  169.     do {
  170.         ModalDialog(nil, &itemHit);
  171.     } while (itemHit != okButton);
  172.  
  173.     CloseDialog(theDialog);
  174.  
  175.     SetPort(savePort);
  176.     return;
  177. }
  178.  
  179. /*------------------------------------------------------*/
  180. /*    init().                                                */
  181. /*------------------------------------------------------*/
  182.  
  183. void init()
  184. {
  185.     OSErr                err;
  186.     int                    i;
  187.     long                QDfeature, OSfeature;
  188.  
  189.     /*    Initialize Managaer.    */
  190.     InitGraf(&qd.thePort);
  191.     FlushEvents(everyEvent, 0);
  192.     InitWindows();
  193.     InitDialogs(nil);
  194.     InitCursor();
  195.  
  196.     /*    Set up menus.    */
  197.     mymenu0 = GetMenu(appleID);
  198.     AddResMenu(mymenu0, 'DRVR');
  199.     InsertMenu(mymenu0,0);
  200.     mymenu1 = GetMenu(fileID);
  201.     InsertMenu(mymenu1,0);
  202.     DrawMenuBar();
  203.     
  204.     /*    Set up variables.    */
  205.     startlocation = 50;
  206.     for (i = 0; i < MAXWINDOWS; i++) {
  207.         playingMovie[i] = FALSE;
  208.         myWindow[i] = nil;
  209.     }
  210.     mycolors = GetCTable (72);
  211.     if (mycolors == nil) Debugger();
  212.     srcPalette = NewPalette (((**mycolors).ctSize)+1, mycolors, pmTolerant, 0);
  213.  
  214.     /*    Use Gestalt to find if QuickDraw and QuickTime is available.    */
  215.     if ((GetTrapAddress(Gestalttest) != GetTrapAddress(NoTrap))) {
  216.         err = Gestalt(gestaltQuickdrawVersion, &QDfeature);
  217.         if (err)
  218.             ExitToShell();
  219.         err = Gestalt(gestaltSystemVersion, &OSfeature);
  220.         if (err)
  221.             ExitToShell();
  222.         if (!DoneFlag && (QDfeature & 0x0f00) != 0x0200 && OSfeature < 0x0607)
  223.             ExitToShell();
  224.         err = Gestalt(gestaltQuickTime, &QDfeature);
  225.         if (err)
  226.             ExitToShell();
  227.     } else
  228.         ExitToShell();
  229.     
  230.     /*    Open QuickTime last.    */
  231.     err = EnterMovies();
  232.     if (err)
  233.         ExitToShell();
  234. }
  235.  
  236. /*------------------------------------------------------*/
  237. /*    finish().                                            */
  238. /*------------------------------------------------------*/
  239.  
  240. void finish()
  241. {
  242.     int i;
  243.  
  244.     for (i = 0; i < MAXWINDOWS; i++)
  245.         if (playingMovie[i] == TRUE) {
  246.             CloseComponent(mcPlay[i]);
  247.             DisposeMovie(myMovie[i]);
  248.             DisposeWindow(myWindow[i]);
  249.              playingMovie[i] = FALSE;
  250.         }
  251.     
  252.     ExitMovies();
  253.     ExitToShell();
  254. }
  255.  
  256. /*------------------------------------------------------*/
  257. /*    doOpenCommand().                                        */
  258. /*------------------------------------------------------*/
  259.  
  260. void doOpenCommand()
  261. {
  262.     int            i, useThisIndex;
  263.     
  264.     useThisIndex = -1;
  265.     
  266.     /*    Search for the first window that is nil.    */
  267.     for (i = MAXWINDOWS-1; i >= 0; i--)
  268.         if (myWindow[i] == nil)
  269.             useThisIndex = i;
  270.             
  271.     /*    If index = -1, then it means that there are no windows avaiable.    */
  272.     if (useThisIndex != -1) {
  273.         myMovie[useThisIndex] = getMovieFromFile();
  274.         if (myMovie[useThisIndex] != 0) {
  275.             playMovie(useThisIndex);
  276.             playingMovie[useThisIndex] = TRUE;
  277.         }
  278.     }
  279. }
  280.  
  281. /*------------------------------------------------------*/
  282. /*    doFlattenCommand().                                        */
  283. /*------------------------------------------------------*/
  284.  
  285. void doFlattenCommand()
  286. {
  287.     int            i, useThisIndex;
  288.     WindowPtr    myTempWindow;
  289.     
  290.     /*    Flatten movie that is currently selected.    */
  291.     myTempWindow = FrontWindow();
  292.     if (myTempWindow == nil) return;
  293.     for (i = 0; i < MAXWINDOWS; i++)
  294.         if (myWindow[i] == myTempWindow)
  295.             flatten(i);
  296. }
  297.  
  298. /*------------------------------------------------------*/
  299. /*    doCloseCommand().                                        */
  300. /*------------------------------------------------------*/
  301.  
  302. void doCloseCommand()
  303. {
  304.     int        i, useThisIndex;
  305.     WindowPtr    myTempWindow;
  306.  
  307.     /*    Close selected window.    */
  308.     myTempWindow = FrontWindow();
  309.     if (myTempWindow == nil) return;
  310.     for (i = 0; i < MAXWINDOWS; i++)
  311.         if (myWindow[i] == myTempWindow) {
  312.             CloseComponent(mcPlay[i]);
  313.             DisposeMovie(myMovie[i]);
  314.             DisposeWindow(myTempWindow);
  315.             playingMovie[i] = FALSE;
  316.             myWindow[i] = nil;
  317.         }
  318. }
  319.  
  320. /*------------------------------------------------------*/
  321. /*    doCommand().                                        */
  322. /*------------------------------------------------------*/
  323.  
  324. void doCommand(mResult)
  325.     long    mResult;
  326. {
  327.     int                     theMenu, theItem;
  328.     char                    daName[256];
  329.     GrafPtr                 savePort;
  330.  
  331.     theItem = LoWord(mResult);
  332.     theMenu = HiWord(mResult);
  333.     
  334.     switch (theMenu) {
  335.         case appleID:
  336.             if (theItem == aboutMeCommand)
  337.                 showAboutMeDialog();
  338.             else {
  339.                 GetItem(mymenu0, theItem, daName);
  340.                 GetPort(&savePort);
  341.                 (void) OpenDeskAcc(daName);
  342.                 SetPort(savePort);
  343.             }
  344.             break;
  345.  
  346.         case fileID:
  347.             switch (theItem) {
  348.                 case openCommand:
  349.                     doOpenCommand();
  350.                     break;
  351.                 case flattenCommand:
  352.                     doFlattenCommand();
  353.                     break;
  354.                 case closeCommand:
  355.                     doCloseCommand();
  356.                     break;
  357.                 case quitCommand:
  358.                     DoneFlag = TRUE;
  359.                     break;
  360.                 default:
  361.                     break;
  362.                 }
  363.             break;
  364.     }
  365.     HiliteMenu(0);
  366.     return;
  367. }
  368.  
  369. /*------------------------------------------------------*/
  370. /*    playMovies().                                            */
  371. /*------------------------------------------------------*/
  372.  
  373. int playMovies(EventRecord *myEvent)
  374. {
  375.     int        i;
  376.     
  377.     for (i = 0; i < MAXWINDOWS; i++)
  378.         if (playingMovie[i] == TRUE) {
  379.             if (MCIsPlayerEvent(mcPlay[i], myEvent))
  380.                 return(TRUE);
  381.         }
  382.     return(FALSE);
  383. }
  384.  
  385. /*------------------------------------------------------*/
  386. /*    main().                                                */
  387. /*------------------------------------------------------*/
  388.  
  389. main()
  390. {
  391.     int                    i;
  392.     char            key;
  393.     Boolean            track;
  394.     long            growResult;
  395.     EventRecord     myEvent;
  396.     WindowPtr        whichWindow;
  397.     int                yieldTime;
  398.     int                ret;
  399.  
  400.     init();
  401.     yieldTime = 0;
  402.     for ( ;; ) {
  403.     
  404.         /*    We can't just do ExitToShell because we must cann ExitMovies.    */
  405.         if (DoneFlag)
  406.             finish();
  407.             
  408.         ret = WaitNextEvent(everyEvent, &myEvent, yieldTime, nil);
  409.         
  410.         if (playMovies(&myEvent))
  411.                 continue;
  412.                 
  413.         if (ret) {
  414.         
  415.             switch (myEvent.what) {
  416.                 case mouseDown:
  417.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  418.                         case inSysWindow:
  419.                             SystemClick(&myEvent, whichWindow);
  420.                             break;
  421.                         case inMenuBar:
  422.                             doCommand(MenuSelect(myEvent.where));
  423.                             break;
  424.                         case inContent:
  425.                             SelectWindow(whichWindow);
  426.                             break;
  427.                         case inDrag:
  428.                             DragWindow (whichWindow, myEvent.where, &qd.screenBits.bounds);
  429.                             break;
  430.                         case inGrow:
  431.                             break;
  432.                         case inGoAway:
  433.                             track = TrackGoAway (whichWindow, myEvent.where);
  434.                             if (track)    doCloseCommand();
  435.                             break;
  436.                         case inZoomIn:
  437.                             break;
  438.                         case inZoomOut:
  439.                             break;
  440.                         default:
  441.                             break;
  442.                     }
  443.                     break;
  444.                 case keyDown:
  445.                 case autoKey:
  446.                     key = myEvent.message & charCodeMask;
  447.                     if ( myEvent.modifiers & cmdKey )
  448.                         if ( myEvent.what == keyDown )
  449.                             doCommand(MenuKey(key));
  450.                     break;
  451.                 case updateEvt:
  452.                     for (i = 0; i < MAXWINDOWS; i++)
  453.                         if ((WindowPtr) myEvent.message == myWindow[i]) {
  454.                             BeginUpdate((WindowPtr) myWindow[i]);
  455.                             EndUpdate((WindowPtr) myWindow[i]);
  456.                         }
  457.                     break;
  458.                 case diskEvt:
  459.                     break;
  460.                 case activateEvt:
  461.                     break;
  462.                 case app4Evt:
  463.                     break; 
  464.                 default:
  465.                     break;
  466.             }
  467.         }
  468.     }
  469. }